
class Progress(object):
	actions=[]
	title=''
	def __new__(self):
		if not '_instance' in self.__dict__:
			self._instance = object.__new__(self)

		return self._instance
	def reset(self):
		self.actions=[]
		self.title=[]
	def add(self,action,title):
		self.actions.append([action,title])
	def process(self):
		actual=1
		count=len(self.actions)
		print self.title
		for a in self.actions:
			print '('+str(actual)+'/'+str(count)+') ',
			print a[1]
			eval(a[0][0])(self,a[0][1])
			actual+=1
		print 'Finished:',self.title
	Log().save()
		
def copy(s,dest):
	for d in Private().files[s]:
		destdir=os.path.join(dest,os.path.split(d)[0])
		Progress().add(['_copy',[d,destdir]],'Installing '+d)
def _copy(p,params):
	d=params[0]
	destdir=params[1]
	if not os.path.exists(destdir):
		os.makedirs(destdir)
	cmd='install '+d+' '+destdir+os.sep
	print cmd
	Log().installed.append(os.path.join(destdir,os.path.basename(d)))
	if os.system(cmd)!=0:
#		Log().save()
#		error('Unknown error encountered, stopping.')
		pass

def copy_bin(dest='',symlink=''):
	Progress().add(['_copy_bin',[dest,symlink]],'Installing binaries')

def _copy_bin(p,params):
	dest=params[0]
	symlink=params[1]
	if dest=='':
		dest=Option().bindir
	cmd='install '+Option().binname+' '+dest+os.sep
	Log().installed.append(os.path.join(dest,Option().package))
	print cmd
	if os.system(cmd)!=0:
#		Log().save()
#		error('Unknown error encountered, stopping.')
		pass
	if symlink!='':
		cmd='ln -s '+dest+os.sep+Option().package+' '+symlink+os.sep+Option().package
		Log().installed.append(os.path.join(symlink,Option().package))
		print cmd
		if os.system(cmd)!=0:
			#Log().save()
			#error('Unknown error encountered, stopping.')
			pass

def init_install(title=''):
	Progress().reset()
	Progress().title=title

def process_install():
	Progress().process()


